package com.shhm.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.shhm.common.dto.request.admin.RolePermsReq;
import com.shhm.common.dto.request.admin.SysUserRoleReq;
import com.shhm.common.dto.response.admin.SysRoleOperatePermsVO;
import com.shhm.common.dto.response.admin.SysUserRoleVO;
import com.shhm.common.entity.admin.SysRoleOperatePerms;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author JackZ
 * @version 1.0
 * @description: TODO
 * @date 2025/8/1 下午4:06
 */
@Mapper
public interface SysRoleOperatePermsMapper extends BaseMapper<SysRoleOperatePerms>
{
    PageDTO<SysRoleOperatePermsVO> pageList(IPage<SysRoleOperatePermsVO> page);

    PageDTO<SysRoleOperatePermsVO> pageSearchList(IPage<SysRoleOperatePermsVO> page,@Param("params") SysRoleOperatePermsVO queryDto);

    @Update("UPDATE sys_role_operate_perms as srop SET match_method=#{params.matchMethod},operate_str=#{params.operateStr},request_uri=#{params.requestUri},role_key=#{params.roleKey},target_desc=#{params.targetDesc}" +
            "where rp_id=#{params.rpId} AND EXISTS(select 1 from sys_role where role_key=#{params.roleKey}) ")
    int edit(@Param("params") SysRoleOperatePerms entity);

    @Insert("insert into sys_role_operate_perms(operate_str,match_method,request_uri,role_key,target_desc)" +
            "select #{params.operateStr},#{params.matchMethod},#{params.requestUri},role_key,#{params.targetDesc} from sys_role where EXISTS(select 1 from sys_role where role_key=#{params.roleKey}) and role_key=#{params.roleKey}" +
            "and not EXISTS(select 1 from sys_role_operate_perms where role_key=#{params.roleKey} and request_uri=#{params.requestUri} and operate_str=#{params.operateStr})")
    int add(@Param("params") SysRoleOperatePerms entity);

    @Insert("INSERT INTO sys_role_operate_perms (role_key,match_method, request_uri, operate_str, target_desc) " +
            "SELECT r.role_key,#{matchMethod}, #{requestUri}, #{operateStr}, m.menu_name " +
            "FROM sys_role r, sys_menu m " +
            "WHERE r.role_id = #{roleId} " +
            "  AND m.menu_id = #{menuId} " +
            "  AND r.role_id = #{roleId} " +
            "  AND m.menu_id = #{menuId}")
    int addData(RolePermsReq entity);

    int updateData(RolePermsReq entity);

    List<SysRoleOperatePerms> selectRoleOperatePerms();

    void resetUserRole(@Param("roleIds") List<Object> roleIds);
}
